package org.jader.bolao.gui.swing.table;

import javax.swing.table.DefaultTableModel;

import org.jader.bolao.GamePlayer;

public abstract class GamePlayersTableModel extends DefaultTableModel {
	private static final long serialVersionUID = 1L;

	public enum COLUMNS {
		POSITION,
		PLAYER,
		TEAM,
		POINTS,
		DETAILED,
		SUMMED,
		;
		public String toString() {
			String s = null;
			switch(this) {
			case POSITION:
				s = "Pos";
				break;
			case PLAYER:
				s = "Jogador";
				break;
			case TEAM:
				s = "Clube";
				break;
			case POINTS:
				s = "Pontos";
				break;
			case DETAILED:
				s = "Detalhes";
				break;
			case SUMMED:
				s = "Somar";
				break;
			}
			return s;
		}
	}
	
	public abstract GamePlayer getPlayerByRow(int row);
	
	protected abstract COLUMNS getColumnByNumber(int col);
    
    public Object getValueAt(int row, int col) {
    	return getValueAt(row, getColumnByNumber(col));
    }
    
    private Object getValueAt(int row, COLUMNS col) {
    	GamePlayer gp = getPlayerByRow(row);
    	switch(col) {
    	case POSITION:
    		return row + 1;
    	case PLAYER:
    		return gp.player();
    	case TEAM:
    		return gp.player().team();
    	case POINTS:
    		return gp.getSum();
    	case DETAILED:
    		return gp.isDetailed();
    	case SUMMED:
    		return gp.isSummed();
    	}
    	return "";
    }
    
    public boolean isCellEditable(int row, int col) {
    	COLUMNS cols = getColumnByNumber(col);
    	if (cols == COLUMNS.DETAILED || cols == COLUMNS.SUMMED) {
    		return true;
    	}
    	else if (cols == COLUMNS.POINTS) {
    		return !(Boolean)getValueAt(row, COLUMNS.DETAILED);
    	}
    	else
    		return false;
    }
    
    @SuppressWarnings("unchecked")
	public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }
    
    public void setValueAt(Object value, int row, int col) {
    	GamePlayer gp = getPlayerByRow(row);
    	COLUMNS cols = getColumnByNumber(col);
    	switch(cols) {
    	case SUMMED:
    		gp.setSummed((Boolean)value);
    		break;

    	case DETAILED:
    		boolean detailed = (Boolean)value;
    		gp.setDetailed(detailed);
    		break;
    		
    	case POINTS:
    		gp.setSum((Integer) value);
    		break;
    	}
    }

	@Override
	public abstract int getColumnCount();

	@Override
	public String getColumnName(int column) {
		COLUMNS cols = getColumnByNumber(column);
		return cols.toString();
	}

	@Override
	public abstract int getRowCount();
}
